package protocol;

import org.json.simple.JSONObject;

public class HTTPRequest implements HTTPPartInterface {
	private HTTPRequestHeaders fHTTPHttpRequestHeaders;
	private HTTPMSG fHttpmsg;

	public HTTPRequest(String msg) {
		String decoded = java.net.URLDecoder.decode(msg);
		 decoded = decoded.replaceAll("(?i)(\n)*(\r\n)*\\$\\Z", "");


		this.fHTTPHttpRequestHeaders = new HTTPRequestHeaders(decoded);
		this.fHttpmsg = new HTTPMSG(decoded);
	}

	public HTTPRequestHeaders getfHTTPHttpRequestHeaders() {
		return fHTTPHttpRequestHeaders;
	}

	protected void setfHTTPHttpRequestHeaders(
			HTTPRequestHeaders fHTTPHttpRequestHeaders) {
		this.fHTTPHttpRequestHeaders = fHTTPHttpRequestHeaders;
	}

	public HTTPMSG getfHttpmsg() {
		return fHttpmsg;
	}

	protected void setfHttpmsg(HTTPMSG fHttpmsg) {
		this.fHttpmsg = fHttpmsg;
	}

	@Override
	public boolean isValid() {
		return this.fHTTPHttpRequestHeaders.isValid()
				&& this.fHttpmsg.isValid();
	}

	@SuppressWarnings("unchecked")
	@Override
	public JSONObject toJSONObject() {
		JSONObject ret = new JSONObject();
		ret.put(HTTP.HEADERS, this.fHTTPHttpRequestHeaders.toJSONObject());
		ret.put(HTTP.MESSAGE, this.fHttpmsg.toJSONObject());
		return ret;
	}

	public String toString() {
		return this.fHTTPHttpRequestHeaders.toString() + "\n" + this.fHttpmsg
				+ "\n$";
	}

}
